# Volume control example
#
no_logfile = true;

screen_width     = 1024;
screen_height    = 768;
screen_bit_depth = 32;

response_matching    = simple_matching;
response_port_output = false; 
write_codes          = false;

active_buttons       = 0;

default_background_color = 0, 0, 0;
default_font             = "arial";
default_font_size        = 20;
default_text_color       = 235, 235, 235;

# PCL code
begin;

picture {
	# indicator
	 arrow_graphic {
		coordinates = 0, -20, 0, -49;
      line_width = 5;
      head_width = 10;
      head_length = 10;
   } indicator;
	x = 0;
	y = 0;
	
	# text
	text {
		caption ="volume";
		font_size = 32;
	} audiovolume;
	x = 0;
	y = 50;
	
	line_graphic {
		coordinates = -382, -50, 126, -50;
	} horizontalLine;
	x = 0;
	y = 0;
} volumePicture;



begin_pcl;

# include libraries
include "stringfunctions.include.pcl";
include "audiovolume.include.pcl";

int x;
int old_x;

# set mouse range
double gainmin = -95.5;
double gainmax = 31.5;
mouse mymouse = response_manager.get_mouse( 1 );
mymouse.set_min_max(1, int(2.0 * gainmin), int(2.0 * gainmax));

# serial port
output_port audio_volume_port = output_port_manager.get_port(1);   

loop until false
begin
	# get x coordinate of the mouse
	mymouse.poll();
	x = mymouse.x();
	
	# adjust volume only when the mouse moved
	if (x != old_x)
	then
		double gain = 0.5 * double(x);
		av_setgain(audio_volume_port, gain, gain);
		
		audiovolume.set_caption(printf(gain, "Gain %0.1f dB"));
		audiovolume.redraw();
		
		# move indicator to the new coordinate
		volumePicture.set_part_x(1, 2*x);
		
		volumePicture.present();
		old_x = x;
	end;
end;